﻿using AngleSharp.Dom;
using AutoMapper;
using Microsoft.AspNetCore.Http;
using Microsoft.Extensions.DependencyInjection;
using Microsoft.Extensions.Logging;
using SqlSugar;
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Linq;
using System.Linq.Expressions;
using System.Reflection;
using System.Text;
using System.Threading.Tasks;
using WomanMotion.WebApi.Infrastructure.AppSettings;
using WomanMotion.WebApi.Infrastructure.DataBaseCommon;
using WomanMotion.WebApi.Infrastructure.ServiceProviderInstance;
using WomanMotion.WebApi.Infrastructure.User;
using WomanMotion.WebApi.Infrastructure.YitId;
using WomanMotion.WebApi.Model.Dtos.Action;
using WomanMotion.WebApi.Model.Dtos.Users;
using WomanMotion.WebApi.Model.Entitys;
using WomanMotion.WebApi.Repository.IRepository;
using WomanMotion.WebApi.Repository.Repository;
using WomanMotion.WebApi.Service.IService;

namespace WomanMotion.WebApi.Service.Service
{
    /// <summary>
    /// 带增删改查的基类
    /// </summary>
    /// <typeparam name="TEntity">数据库实体</typeparam>
    /// <typeparam name="TVM">前端返回VM</typeparam>
    /// <typeparam name="TUpdataDTO">更新数据DTO</typeparam>
    /// <typeparam name="TCreatedDTO">添加数据DTO</typeparam>
    public class BaseCRUDService<TEntity, TVM, TUpdataDTO, TCreatedDTO> : BaseService, IBaseCRUDService<TEntity, TVM, TUpdataDTO, TCreatedDTO> where TEntity : class
    {
        //protected IHttpContextAccessor httpContext = ServiceProviderInstance.Instance.GetRequiredService<IHttpContextAccessor>();

        //从子类构造函数中传入
        protected IBaseRepository<TEntity> _iBaseRepository;

        protected IHttpContextAccessor httpContext;

        /// <summary>
        /// HttpContext对象里面的User对象
        /// </summary>
        protected UserContext userContext
        {
            get
            {
                var item = new UserContext();
                var userid = AppSettingsHelper.userId;
                //当HttpContext等于null的时候说明是系统初始化直接取admin的id
                if (httpContext.HttpContext == null)
                {
                    item.UserId = userid.AdminId;
                    item.UserIdExistNull = null;
                    item.UserName = null;
                    item.RoleCode = null;
                }
                else
                {
                    try
                    {
                        item.UserId = httpContext.HttpContext.User.Claims.Where(m => m.Type == "UserId").First().Value;
                        item.UserIdExistNull = httpContext.HttpContext.User.Claims.Where(m => m.Type == "UserId").First().Value;
                        item.UserName = httpContext.HttpContext.User.Claims.Where(m => m.Type == "UserName").First().Value;
                        item.RoleCode = httpContext.HttpContext.User.Claims.Where(m => m.Type == "RoleCode").First().Value;
                    }
                    catch (Exception ex)
                    {
                        item.UserId = userid.AdminId;
                        item.UserIdExistNull = null;
                        item.UserName = null;
                        item.RoleCode = null;
                    }
                }
                return item;
            }
            set { }
        }

        public BaseCRUDService(ILogger logger, IMapper mapper, IBaseRepository<TEntity> baseRepository, IHttpContextAccessor _httpContext) : base(logger, mapper)
        {
            this._iBaseRepository = baseRepository;
            this.httpContext = _httpContext;
        }

        #region 添加

        /// <summary>
        /// 添加一条数据(异步)
        /// </summary>
        /// <param name="dto">dto</param>
        /// <returns></returns>
        public virtual async Task<TVM> CreatedAsync(TCreatedDTO dto)
        {
            var userid = userContext.UserId.ToString();
            var entity = _mapper.Map<TCreatedDTO, TEntity>(dto);
            //通过反射给属性赋值
            Type type = typeof(TEntity);
            // 获取所有的实例成员
            var fileds = type.GetProperties();
            // 给属性赋值
            foreach (var item in fileds)
            {
                if (item.Name.Equals("Id"))
                {
                    item.SetValue(entity, YitIdHelper.NextId());
                }
                if (item.Name.Equals("Ifdisable"))
                {
                    item.SetValue(entity, 1);
                }
                if (item.Name.Equals("Createdby"))
                {
                    item.SetValue(entity, long.Parse(userid));
                }
                if (item.Name.Equals("Createdtime"))
                {
                    item.SetValue(entity, DateTime.Now);
                }
                if (item.Name.Equals("Updatedby"))
                {
                    item.SetValue(entity, long.Parse(userid));
                }
                if (item.Name.Equals("Updatedtime"))
                {
                    item.SetValue(entity, DateTime.Now);
                }
            }
            var res = await _iBaseRepository.InsertAsync(entity);
            var vm = _mapper.Map<TEntity, TVM>(res);
            return vm;
        }

        /// <summary>
        ///添加一条数据,传入一个表达式判断数据是否存在，如果存在就返回原有数据，不存在就新增(异步)
        /// </summary>
        /// <param name="dto"></param>
        /// <param name="expression"></param>
        /// <returns></returns>
        public virtual async Task<TVM> CreatedAsync(TCreatedDTO dto, Expression<Func<TEntity, bool>> expression)
        {
            var userid = userContext.UserId.ToString();
            //查询是否存在记录
            var existence = await _iBaseRepository.GetAsync(expression);
            //当存在数据直接返回
            if (existence != null)
            {
                var vm = _mapper.Map<TEntity, TVM>(existence);
                return vm;
            }
            else//当数据不存在
            {
                var entity = _mapper.Map<TCreatedDTO, TEntity>(dto);
                //通过反射给属性赋值
                Type type = typeof(TEntity);
                // 获取所有的实例成员
                var fileds = type.GetProperties();
                // 给属性赋值
                foreach (var item in fileds)
                {
                    if (item.Name.Equals("Id"))
                    {
                        item.SetValue(entity, YitIdHelper.NextId());
                    }
                    if (item.Name.Equals("Ifdisable"))
                    {
                        item.SetValue(entity, 1);
                    }
                    if (item.Name.Equals("Createdby"))
                    {
                        item.SetValue(entity, long.Parse(userid));
                    }
                    if (item.Name.Equals("Createdtime"))
                    {
                        item.SetValue(entity, DateTime.Now);
                    }
                    if (item.Name.Equals("Updatedby"))
                    {
                        item.SetValue(entity, long.Parse(userid));
                    }
                    if (item.Name.Equals("Updatedtime"))
                    {
                        item.SetValue(entity, DateTime.Now);
                    }
                }
                var res = await _iBaseRepository.InsertAsync(entity);
                var vm = _mapper.Map<TEntity, TVM>(res);
                return vm;
            }
        }

        #endregion 添加

        #region 删除

        /// <summary>
        /// 根据主键删除数据(异步)
        /// </summary>
        /// <param name="id">主键id</param>
        /// <returns></returns>
        public virtual async Task<int> DeleteAsync(object id)
        {
            var res = await this._iBaseRepository.DeleteAsync(id);
            return res;
        }

        /// <summary>
        /// 根据主键数组批量删除数据(异步)
        /// </summary>
        /// <param name="ids">主键id数组</param>
        /// <returns></returns>
        public virtual async Task<int> DeleteAsync(object[] ids)
        {
            var res = await this._iBaseRepository.DeleteAsync(ids);
            return res;
        }

        /// <summary>
        /// 根据条件删除数据(异步)
        /// </summary>
        /// <param name="expression"></param>
        /// <returns></returns>
        public virtual async Task<int> DeleteAsync(Expression<Func<TEntity, bool>> expression)
        {
            var res = await this._iBaseRepository.DeleteAsync(expression);
            return res;
        }

        #endregion 删除

        #region 更新

        /// <summary>
        /// 启用或禁用单条数据(异步)
        /// </summary>
        /// <param name="ifdb">禁用还是启用</param>
        /// <param name="id">主键id</param>
        /// <returns>影响行数</returns>
        public virtual async Task<int> UpdateDisableAsync(DisableEnum ifdb, long id)
        {
            var userid = userContext.UserId.ToString();
            var entity = await this._iBaseRepository.GetByIDAsync(id);
            if (entity == null)
            {
                return 0;
            }
            //通过反射给属性赋值
            Type type = typeof(TEntity);
            // 获取所有的实例成员
            var fileds = type.GetProperties();
            // 给属性赋值
            foreach (var item in fileds)
            {
                if (item.Name.Equals("Ifdisable"))
                {
                    if (ifdb == DisableEnum.Enable)
                    {
                        item.SetValue(entity, 1);
                    }
                    else
                    {
                        item.SetValue(entity, 0);
                    }
                }
                if (item.Name.Equals("Updatedtime"))
                {
                    item.SetValue(entity, DateTime.Now);
                }
                if (item.Name.Equals("Updatedby"))
                {
                    item.SetValue(entity, long.Parse(userid));
                }
            }
            var res = await this._iBaseRepository.UpdateAsync(entity);
            return res;
        }

        /// <summary>
        /// 启用或禁用多条数据(异步)
        /// </summary>
        /// <param name="ifdb">禁用还是启用</param>
        /// <param name="id">主键id</param>
        /// <returns>影响行数</returns>
        public virtual async Task<int> UpdateDisableAsync(DisableEnum ifdb, long[] id)
        {
            var userid = userContext.UserId.ToString();
            List<TEntity> entitys = new List<TEntity>();
            foreach (var itemid in id)
            {
                var entity = await this._iBaseRepository.GetByIDAsync(itemid);
                if (entity == null)
                {
                    return 0;
                }
                //通过反射给属性赋值
                Type type = typeof(TEntity);
                // 获取所有的实例成员
                var fileds = type.GetProperties();
                // 给属性赋值
                foreach (var item in fileds)
                {
                    if (item.Name.Equals("Ifdisable"))
                    {
                        if (ifdb == DisableEnum.Enable)
                        {
                            item.SetValue(entity, 1);
                        }
                        else
                        {
                            item.SetValue(entity, 0);
                        }
                    }
                    if (item.Name.Equals("Updatedtime"))
                    {
                        item.SetValue(entity, DateTime.Now);
                    }
                    if (item.Name.Equals("Updatedby"))
                    {
                        item.SetValue(entity, long.Parse(userid));
                    }
                }
                entitys.Add(entity);
            }
            var res = await this._iBaseRepository.UpdateAsync(entitys);
            return res;
        }

        /// <summary>
        /// 更新单条数据(异步)
        /// </summary>
        /// <param name="entity">需要更新的实体</param>
        /// <returns>影响行数</returns>
        public virtual async Task<TVM> UpdateAsync(TUpdataDTO dto)
        {
            var userid = userContext.UserId.ToString();
            //dto转换实体
            var entity = _mapper.Map<TUpdataDTO, TEntity>(dto);
            DateTime createdtime = DateTime.Now;
            long createdby = 0;
            long id = 0;
            //获取dto的id
            Type typedto = typeof(TUpdataDTO);
            var filedsdto = typedto.GetProperties();
            foreach (var item in filedsdto)
            {
                if (item.Name.Equals("Id"))
                {
                    id = (long)item.GetValue(dto, null);
                }
            }
            //通过ID查询实体
            var entitytemp = await this._iBaseRepository.GetByIDAsync(id);
            if (entitytemp == null)
            {
                return default(TVM);
            }
            //获取查询实体的创建人和创建时间
            Type typeentitytemp = typeof(TEntity);
            var filedsentitytemp = typeentitytemp.GetProperties();
            foreach (var item in filedsentitytemp)
            {
                if (item.Name.Equals("Createdby"))
                {
                    createdby = (long)item.GetValue(entitytemp, null);
                }
                if (item.Name.Equals("Createdtime"))
                {
                    createdtime = (DateTime)item.GetValue(entitytemp, null);
                }
            }
            //通过反射给属性赋值
            Type type = typeof(TEntity);
            // 获取所有的实例成员
            var fileds = type.GetProperties();
            // 给属性赋值
            foreach (var item in fileds)
            {
                if (item.Name.Equals("Updatedtime"))
                {
                    item.SetValue(entity, DateTime.Now);
                }
                if (item.Name.Equals("Updatedby"))
                {
                    item.SetValue(entity, long.Parse(userid));
                }
                if (item.Name.Equals("Createdby"))
                {
                    item.SetValue(entity, createdby);
                }
                if (item.Name.Equals("Createdtime"))
                {
                    item.SetValue(entity, createdtime);
                }
            }

            var res = await this._iBaseRepository.UpdateAsync(entity);
            if (res > 0)
            {
                var vm = _mapper.Map<TEntity, TVM>(entity);
                return vm;
            }
            else
            {
                return default(TVM);
            }
        }

        /// <summary>
        /// 更新单条数据传入实体更新实体(异步)
        /// </summary>
        /// <param name="entity">需要更新的实体</param>
        /// <returns>影响行数</returns>
        public virtual async Task<TEntity> UpdateEntityAsync(TEntity entity)
        {
            var userid = userContext.UserId.ToString();
            DateTime createdtime = DateTime.Now;
            //通过反射给属性赋值
            Type type = typeof(TEntity);
            // 获取所有的实例成员
            var fileds = type.GetProperties();
            // 给属性赋值
            foreach (var item in fileds)
            {
                if (item.Name.Equals("Updatedtime"))
                {
                    item.SetValue(entity, DateTime.Now);
                }
                if (item.Name.Equals("Updatedby"))
                {
                    item.SetValue(entity, long.Parse(userid));
                }
            }

            var res = await this._iBaseRepository.UpdateAsync(entity);
            if (res > 0)
            {
                return entity;
            }
            else
            {
                return default(TEntity);
            }
        }

        /// <summary>
        /// 批量更新数据(异步)
        /// </summary>
        /// <param name="dtos">dtos</param>
        /// <returns>返回vm</returns>
        /// <exception cref="Exception">更新失败</exception>
        public virtual async Task<List<TVM>> UpdateAsync(List<TUpdataDTO> dtos)
        {
            var userid = userContext.UserId.ToString();
            //dto转换实体
            var entitystemp = _mapper.Map<List<TUpdataDTO>, List<TEntity>>(dtos);
            //创建一个临时对象存储实体
            List<TEntity> entitys = new List<TEntity>();
            List<TVM> vms = new List<TVM>();
            foreach (var itementitytemp in entitystemp)
            {
                DateTime createdtime = DateTime.Now;
                long createdby = 0;
                long id = 0;
                //获取dto的id
                Type typedto = typeof(TEntity);
                var filedsdto = typedto.GetProperties();
                foreach (var item in filedsdto)
                {
                    if (item.Name.Equals("Id"))
                    {
                        id = (long)item.GetValue(itementitytemp, null);
                    }
                }
                //通过ID查询实体
                var entitytemp = await this._iBaseRepository.GetByIDAsync(id);
                if (entitytemp == null)
                {
                    return default(List<TVM>);
                }
                //获取查询实体的创建人和创建时间
                Type typeentitytemp = typeof(TEntity);
                var filedsentitytemp = typeentitytemp.GetProperties();
                foreach (var item in filedsentitytemp)
                {
                    if (item.Name.Equals("Createdby"))
                    {
                        createdby = (long)item.GetValue(entitytemp, null);
                    }
                    if (item.Name.Equals("Createdtime"))
                    {
                        createdtime = (DateTime)item.GetValue(entitytemp, null);
                    }
                }
                //通过反射给属性赋值
                Type type = typeof(TEntity);
                // 获取所有的实例成员
                var fileds = type.GetProperties();
                // 给属性赋值
                foreach (var item in fileds)
                {
                    if (item.Name.Equals("Updatedtime"))
                    {
                        item.SetValue(itementitytemp, DateTime.Now);
                    }
                    if (item.Name.Equals("Updatedby"))
                    {
                        item.SetValue(itementitytemp, long.Parse(userid));
                    }
                    if (item.Name.Equals("Createdby"))
                    {
                        item.SetValue(itementitytemp, createdby);
                    }
                    if (item.Name.Equals("Createdtime"))
                    {
                        item.SetValue(itementitytemp, createdtime);
                    }
                }
                entitys.Add(itementitytemp);
            }
            var res = await this._iBaseRepository.UpdateAsync(entitys);
            if (res > 0)
            {
                vms = _mapper.Map<List<TEntity>, List<TVM>>(entitys);
                return vms;
            }
            else
            {
                return default(List<TVM>);
            }
        }

        #endregion 更新

        #region 查询

        /// <summary>
        /// 查询单个数据(异步)
        /// </summary>
        /// <param name="expression">条件</param>
        /// <returns></returns>
        public virtual async Task<TVM> GetAsync(Expression<Func<TEntity, bool>> expression)
        {
            var res = await this._iBaseRepository.GetAsync(expression);
            var vm = _mapper.Map<TEntity, TVM>(res);
            return vm;
        }

        /// <summary>
        /// 查询单个数据,返回实体(异步)
        /// </summary>
        /// <param name="expression">条件</param>
        /// <returns></returns>
        public virtual async Task<TEntity> GetEntityAsync(Expression<Func<TEntity, bool>> expression)
        {
            var res = await this._iBaseRepository.GetAsync(expression);
            return res;
        }

        /// <summary>
        /// 查询单个带导航数据(异步)
        /// </summary>
        /// <param name="expression">条件</param>
        /// <param name="ignoreProperyNameList">需要忽略的导航属性</param>
        /// <returns></returns>
        public virtual async Task<TVM> GetNavAsync(Expression<Func<TEntity, bool>> expression, params string[] ignoreProperyNameList)
        {
            var res = await this._iBaseRepository.GetNavAsync(expression, ignoreProperyNameList);
            var vm = _mapper.Map<TEntity, TVM>(res);
            return vm;
        }

        /// <summary>
        ///查询单个数据(同步)
        /// </summary>
        /// <param name="expression">条件</param>
        /// <returns></returns>
        public virtual TVM Get(Expression<Func<TEntity, bool>> expression)
        {
            var res = this._iBaseRepository.Get(expression);
            var vm = _mapper.Map<TEntity, TVM>(res);
            return vm;
        }

        /// <summary>
        /// 查询单个带导航数据(同步)
        /// </summary>
        /// <param name="expression"></param>
        /// <param name="ignoreProperyNameList"></param>
        /// <returns></returns>
        public virtual TVM GetNav(Expression<Func<TEntity, bool>> expression, params string[] ignoreProperyNameList)
        {
            var res = this._iBaseRepository.GetNav(expression, ignoreProperyNameList);
            var vm = _mapper.Map<TEntity, TVM>(res);
            return vm;
        }

        /// <summary>
        /// 查询所有数据集合(异步)
        /// </summary>
        /// <returns></returns>
        public virtual async Task<List<TVM>> GetListAsync()
        {
            var res = await this._iBaseRepository.GetListAsync();
            var vm = _mapper.Map<List<TEntity>, List<TVM>>(res);
            return vm;
        }

        /// <summary>
        /// 查询所有带排序数据集合(异步)
        /// </summary>
        /// <param name="orderby">排序数组字符串</param>
        /// <returns></returns>
        public virtual async Task<List<TVM>> GetListOrderByAsync(string[] orderby)
        {
            var res = await this._iBaseRepository.GetListOrderByAsync(orderby);
            var vm = _mapper.Map<List<TEntity>, List<TVM>>(res);
            return vm;
        }

        /// <summary>
        /// 查询所有带导航数据集合(异步)
        /// </summary>
        /// <param name="ignoreProperyNameList">需要忽略的导航属性</param>
        /// <returns></returns>
        public virtual async Task<List<TVM>> GetListNavAsync(params string[] ignoreProperyNameList)
        {
            var res = await this._iBaseRepository.GetListNavAsync(ignoreProperyNameList);
            var vm = _mapper.Map<List<TEntity>, List<TVM>>(res);
            return vm;
        }

        /// <summary>
        /// 查询所有带排序带导航数据集合(异步)
        /// </summary>
        /// <param name="orderby">排序数组字符串</param>
        /// <param name="ignoreProperyNameList">需要忽略的导航属性</param>
        /// <returns></returns>
        public virtual async Task<List<TVM>> GetListNavOrderByAsync(string[] orderby, params string[] ignoreProperyNameList)
        {
            var res = await this._iBaseRepository.GetListNavOrderByAsync(orderby, ignoreProperyNameList);
            var vm = _mapper.Map<List<TEntity>, List<TVM>>(res);
            return vm;
        }

        /// <summary>
        /// 根据主键查询单个数据(异步)
        /// </summary>
        /// <param name="id">id主键</param>
        /// <returns>实体</returns>
        public virtual async Task<TVM> GetByIdAsync(object id)
        {
            var res = await this._iBaseRepository.GetByIDAsync(id);
            var vm = _mapper.Map<TEntity, TVM>(res);
            return vm;
        }

        /// <summary>
        ///根据主键查询单个带导航数据(异步)
        /// </summary>
        /// <param name="id">id主键</param>
        /// <param name="ignoreProperyNameList">需要忽略的导航属性</param>
        /// <returns></returns>
        public virtual async Task<TVM> GetByIDNavAsync(object id, params string[] ignoreProperyNameList)
        {
            var res = await this._iBaseRepository.GetByIDNavAsync(id, ignoreProperyNameList);
            var vm = _mapper.Map<TEntity, TVM>(res);
            return vm;
        }

        /// <summary>
        /// 根据主键查询单个数据(同步)
        /// </summary>
        /// <param name="id">id主键</param>
        /// <returns></returns>
        public virtual TVM GetById(long id)
        {
            var res = this._iBaseRepository.GetByID(id);
            var vm = _mapper.Map<TEntity, TVM>(res);
            return vm;
        }

        /// <summary>
        ///根据主键查询单个带导航数据(同步)
        /// </summary>
        /// <param name="id">id主键</param>
        /// <param name="ignoreProperyNameList">需要忽略的导航属性</param>
        /// <returns></returns>
        public virtual TVM GetByIDNav(object id, params string[] ignoreProperyNameList)
        {
            var res = this._iBaseRepository.GetByIDNav(id, ignoreProperyNameList);
            var vm = _mapper.Map<TEntity, TVM>(res);
            return vm;
        }

        /// <summary>
        /// 按条件返回数据集合(异步)
        /// </summary>
        /// <param name="expression">条件</param>
        /// <returns></returns>
        public virtual async Task<List<TVM>> GetListAsync(Expression<Func<TEntity, bool>> expression)
        {
            var res = await this._iBaseRepository.GetListAsync(expression);
            var vm = _mapper.Map<List<TEntity>, List<TVM>>(res);
            return vm;
        }

        /// <summary>
        ///按条件返回带排序数据集合(异步)
        /// </summary>
        /// <param name="expression">条件</param>
        /// <param name="orderby">排序数组字符串</param>
        /// <returns></returns>
        public virtual async Task<List<TVM>> GetListOrderByAsync(Expression<Func<TEntity, bool>> expression, string[] orderby)
        {
            var res = await this._iBaseRepository.GetListOrderByAsync(expression, orderby);
            var vm = _mapper.Map<List<TEntity>, List<TVM>>(res);
            return vm;
        }

        /// <summary>
        ///按条件返回带导航数据集合(异步)
        /// </summary>
        /// <param name="expression">条件</param>
        /// <param name="ignoreProperyNameList">需要忽略的导航属性</param>
        /// <returns></returns>
        public virtual async Task<List<TVM>> GetListNavAsync(Expression<Func<TEntity, bool>> expression, params string[] ignoreProperyNameList)
        {
            var res = await this._iBaseRepository.GetListNavAsync(expression, ignoreProperyNameList);
            var vm = _mapper.Map<List<TEntity>, List<TVM>>(res);
            return vm;
        }

        /// <summary>
        ///按条件返回带排序带导航数据集合(异步)
        /// </summary>
        /// <param name="expression"条件></param>
        /// <param name="orderby">排序数组字符串</param>
        /// <param name="ignoreProperyNameList">需要忽略的导航属性</param>
        /// <returns></returns>
        public virtual async Task<List<TVM>> GetListNavOrderByAsync(Expression<Func<TEntity, bool>> expression, string[] orderby, params string[] ignoreProperyNameList)
        {
            var res = await this._iBaseRepository.GetListNavOrderByAsync(expression, orderby, ignoreProperyNameList);
            var vm = _mapper.Map<List<TEntity>, List<TVM>>(res);
            return vm;
        }

        /// <summary>
        /// 查询是否有符合记录的数据存在(异步)
        /// </summary>
        /// <param name="expression">表达式</param>
        /// <returns>存在true,不存在false</returns>
        public virtual async Task<bool> GetAnyAsync(Expression<Func<TEntity, bool>> expression)
        {
            return await this._iBaseRepository.GetAnyAsync(expression);
        }

        /// <summary>
        /// 查询存在数据的行数(异步)
        /// </summary>
        /// <param name="expression">表达式</param>
        /// <returns>数据行数</returns>
        public virtual async Task<int> GetCountAsync(Expression<Func<TEntity, bool>> expression)
        {
            return await this._iBaseRepository.GetCountAsync(expression);
        }

        /// <summary>
        /// 分页查询单表数据(异步)
        /// </summary>
        /// <param name="pars">查询参数</param>
        /// <returns>数据集合</returns>
        public virtual async Task<PageViewBase<List<TVM>>> GetPageListAsync(QueryParameterBase pars)
        {
            var data = await _iBaseRepository.GetPageListAsync(pars);
            var res = new PageViewBase<List<TVM>>();
            res.Total = data.Total;
            res.PageNum = pars.PageNum;
            res.PageSize = pars.PageSize;
            var vms = _mapper.Map<List<TEntity>, List<TVM>>(data.Data);
            res.Data = vms;
            return res;
            //var data = await _iBaseRepository.GetPageEntityListAsync(pars);
            //int total = data.Key;
            //var vms = _mapper.Map<List<TEntity>, List<TVM>>(data.Value);
            //var res = new PageViewBase<List<TVM>>();
            //res.Total = total;
            //res.PageNum = pars.PageNum;
            //res.PageSize = pars.PageSize;
            //res.Data = vms;
            //return res;
        }

        /// <summary>
        ///分页查询带导航单表数据(异步)
        /// </summary>
        /// <param name="pars">查询参数</param>
        /// <param name="ignoreProperyNameList">需要忽略的导航属性</param>
        /// <returns></returns>
        public virtual async Task<PageViewBase<List<TVM>>> GetPageNavListAsync(QueryParameterBase pars, params string[] ignoreProperyNameList)
        {
            var data = await _iBaseRepository.GetPageNavListAsync(pars, ignoreProperyNameList);
            var res = new PageViewBase<List<TVM>>();
            res.Total = data.Total;
            res.PageNum = pars.PageNum;
            res.PageSize = pars.PageSize;
            var vms = _mapper.Map<List<TEntity>, List<TVM>>(data.Data);
            res.Data = vms;
            return res;
        }

        /// <summary>
        ///分页获取实体数据的KeyValuePair(异步)
        /// </summary>
        /// <param name="pars">分页查询对象</param>
        /// <returns>key是数据总数,value是数据集合</returns>
        public virtual async Task<KeyValuePair<int, List<TVM>>> GetPageKeyValuePairListAsync(QueryParameterBase pars)
        {
            var data = await _iBaseRepository.GetPageKeyValuePairListAsync(pars);
            var vms = _mapper.Map<List<TEntity>, List<TVM>>(data.Value);
            var res = new KeyValuePair<int, List<TVM>>(data.Key, vms);
            return res;
        }

        /// <summary>
        ///分页获取带导航实体数据的KeyValuePair(异步)
        /// </summary>
        /// <param name="pars">分页查询对象</param>
        /// <param name="ignoreProperyNameList">需要忽略的导航属性</param>
        /// <returns></returns>
        public virtual async Task<KeyValuePair<int, List<TVM>>> GetPageNavKeyValuePairListAsync(QueryParameterBase pars, params string[] ignoreProperyNameList)
        {
            var data = await _iBaseRepository.GetPageNavKeyValuePairListAsync(pars, ignoreProperyNameList);
            var vms = _mapper.Map<List<TEntity>, List<TVM>>(data.Value);
            var res = new KeyValuePair<int, List<TVM>>(data.Key, vms);
            return res;
        }

        #endregion 查询
    }
}